/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package elaborate.h2x.parser;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.tidy.Tidy;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * @author lendle
 */
public class XHTMLUtil {
    public static Document fromHTML(Reader reader) throws ParserConfigurationException, SAXException, IOException{
        Tidy tidy=new Tidy();
        tidy.setXHTML(true);
        StringWriter output=new StringWriter();
        tidy.parse(reader, output);
        
        String docString=output.toString();
        
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setFeature("http://xml.org/sax/features/validation", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        dbf.setValidating(false);
        dbf.setExpandEntityReferences(false);
        DocumentBuilder docBuilder=dbf.newDocumentBuilder();
        StringReader stringReader=new StringReader(docString);
        try{
            Document doc=docBuilder.parse(new InputSource(stringReader));

            return doc;
        }finally{
            stringReader.close();
        }
    }
    
    public static Document fromHTML(URL url, String charset) throws UnsupportedEncodingException, IOException, ParserConfigurationException, SAXException{
        InputStream input = null;
        try {
            input = url.openStream();
            return fromHTML(new InputStreamReader(input, charset));
        } finally {
            try {
                input.close();
            } catch (IOException ex) {
                Logger.getLogger(XHTMLUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
